// pages/order/goods/goods.ts
import { doList } from '~/api/order'
import type { tabsState, optionsState } from '~/../typings/types/goods'
let isRefresh = false
interface queryFormSate {
  pageNo: number
  pageSize: number
  status?: string | number
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stickyProps: {
      zIndex: 2,
    },
    current: 0,
    tabsList: [{
      name: '全部',
      value: 0,
      status: ''
    }, {
      name: '待成交',
      value: 1,
      status: 1
    }, {
      name: '已成交',
      value: 2,
      status: 2
    }, {
      name: '已取消',
      value: 3,
      status: 4
    }] as tabsState[],
    queryForm: {
      pageNo: 1,
      pageSize: 10,
      status: '',
    } as queryFormSate,
    // 分页
    status: 'loading',
    tableData: [] as any[],
  },
  handDetail(e: any) {
    const { tableData } = this.data
    const dataset = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/order/detail/detail?id=${dataset.id}`,
      events: {
        backInfo: (newInfo: any) => {
          const index = tableData.find((el: any) => el.id === dataset.id)
          const info = tableData[index]
          info.status = newInfo.status
          tableData[index] = info
          this.setData({
            tableData
          })
        }
      }
    })
  },
  onTabsChange(e: any) {
    const { queryForm, tabsList } = this.data
    const tabInfo: tabsState = tabsList[e.detail.value]
    queryForm.status = tabInfo.status
    this.setData({
      queryForm,
      current: e.detail.value
    })
    this.onRefresh()
  },
  onRefresh() {
    const { queryForm } = this.data
    queryForm.pageNo = 1
    this.setData({
      status: 'loading',
      tableData: [],
      queryForm
    })
    this.fetchData()
  },
  /**
   * 分页获取数据
   */
  async fetchData() {
    this.setData({
      status: 'loading'
    })
    const { queryForm, tableData } = this.data
    const data = await doList(queryForm)
    let newData = []
    if (queryForm.pageNo === 1) {
      newData = data.list
    } else {
      newData = [...tableData, ...data.list]
    }
    queryForm.pageNo++
    this.setData({
      tableData: newData,
      queryForm,
      status: queryForm.pageSize === data.list.length ? 'loadmore' : 'nomore'
    })
    if (isRefresh) {
      wx.stopPullDownRefresh();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(this: any, e: optionsState) {
    if (e.curren) {
      this.setData({
        current: Number(e.curren)
      })
    }
    const { queryForm, tabsList, current } = this.data
    const tabInfo: tabsState = tabsList[current]
    queryForm.status = tabInfo.status
    this.setData({
      queryForm
    })
    this.fetchData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    isRefresh = true
    const { queryForm } = this.data
    queryForm.pageNo = 1
    this.setData({
      status: 'loading',
      tableData: [],
      queryForm
    })
    this.fetchData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.status == 'nomore') return
    this.fetchData()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})